package com.feishi.core.util.common;

import java.io.*;
import java.util.UUID;

/**
 * @version 1.0
 * @auth caixq
 * @time 18-4-18 17:18
 **/
public class LibraryUtil {
    /**
     * 从环境变量中加载动态链接库
     * */
    public static void load(String path){
        if(!path.endsWith(".dll")&&!path.endsWith(".so")){
            String systemType = System.getProperty("os.name");
            String libExtension = (systemType.toLowerCase().indexOf("win")!=-1) ? ".dll" : ".so";
            path+=libExtension;
        }
        String nativeTempDir = System.getProperty("java.io.tmpdir");

        InputStream in = null;
        File extractedLibFile = new File(nativeTempDir+File.separator+ UUID.randomUUID()+"");

        in = Thread.currentThread().getContextClassLoader().getResourceAsStream("libs/"+path);
        if(in==null)
            in =  Thread.currentThread().getContextClassLoader().getResourceAsStream(path);

        FileUtil.writeStreamToFile(in,extractedLibFile);


        System.load(extractedLibFile.toString());
        //加载后删除文件
        extractedLibFile.delete();
    }
}
